package cn.com.egova.erp.biz.impl;

import cn.com.egova.erp.biz.ISupplierBiz;
import cn.com.egova.erp.dao.ISupplierDao;
import cn.com.egova.erp.entity.Supplier;
import cn.com.egova.erp.exception.ErpException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 供应商业务逻辑类
 *
 * @author Administrator
 */
public class SupplierBiz extends BaseBiz<Supplier> implements ISupplierBiz {

    private ISupplierDao supplierDao;

    public void setSupplierDao(ISupplierDao supplierDao) {
        this.supplierDao = supplierDao;
        super.setBaseDao(this.supplierDao);
    }

    /**
     * 导出供应商/客户
     *
     * @param os
     * @param t1
     */
    @Override
    public void export(OutputStream os, Supplier t1) {
        //获取供应商/客户列表
        List<Supplier> suppliers = supplierDao.getList(t1, null, null);
        //创建工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建工作表
        HSSFSheet sheet = null;
        if (Supplier.TYPE_SUPPLIER.equals(t1.getType())) {
            sheet = wb.createSheet("供应商");
        } else {
            sheet = wb.createSheet("客户");
        }

        //写入表头
        HSSFRow header = sheet.createRow(0);
        // 定义好每一列的标题
        String[] headerNames = {"名称", "地址", "联系人", "电话", "Email"};
        // 指定每一列的宽度
        int[] columnWidths = {4000, 8000, 2000, 3000, 8000};
        HSSFCell cell = null;
        for (int i = 0; i < headerNames.length; i++) {
            cell = header.createCell(i);
            cell.setCellValue(headerNames[i]);
            sheet.setColumnWidth(i, columnWidths[i]);
        }

        //写入内容
        int i = 1;  //从1开始，因为第一行是表头，前边已经创建了
        for (Supplier supplier : suppliers) {
            HSSFRow row = sheet.createRow(i);
            row.createCell(0).setCellValue(supplier.getName());      //名称
            row.createCell(1).setCellValue(supplier.getAddress());   //地址
            row.createCell(2).setCellValue(supplier.getContact());   //联系人
            row.createCell(3).setCellValue(supplier.getTele());      //电话
            row.createCell(4).setCellValue(supplier.getEmail());     //邮箱
            i++;
        }

        try {
            //写入到输出流中
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void doImport(InputStream is) throws IOException {
        HSSFWorkbook wb = null;
        try {
            //从上传文件中读取工作簿
            wb = new HSSFWorkbook(is);
            //读取第一张工作表
            HSSFSheet sheet = wb.getSheetAt(0);
            String type = "";
            if ("供应商".equals(sheet.getSheetName())) {
                type = Supplier.TYPE_SUPPLIER;
            } else if ("客户".equals(sheet.getSheetName())) {
                type = Supplier.TYPE_CUSTOMER;
            } else {
                throw new ErpException("当前导入的工作表名称不正确");
            }
            //读取数据
            //获取最后一行的行号
            int lastRow = sheet.getLastRowNum();
            if (lastRow <= 1) {
                throw new ErpException("工作表中无有效数据");
            }
            //从1开始，因为标题行不用读取
            Supplier supplier = null;
            for (int i = 1; i <= lastRow; i++) {
                //先根据名称查找一下，如果存在则更新，不存在则插入
                supplier = new Supplier();
                supplier.setName(sheet.getRow(i).getCell(0).getStringCellValue());      //供应商名称
                List<Supplier> list = supplierDao.getList(null, supplier, null);
                if (list != null && list.size() > 0) {
                    supplier = list.get(0);
                }
                supplier.setAddress(sheet.getRow(i).getCell(1).getStringCellValue());   //地址
                supplier.setContact(sheet.getRow(i).getCell(2).getStringCellValue());   //联系人
                supplier.setTele(sheet.getRow(i).getCell(3).getStringCellValue());      //电话
                supplier.setEmail(sheet.getRow(i).getCell(4).getStringCellValue());     //邮箱
                //新增
                if (list == null || list.size() == 0) {
                    supplier.setType(type);
                    supplierDao.add(supplier);
                }
            }
        }finally {
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
